package mcsm.DataBaseIntegration;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import mcsm.DataBaseInterface.EditOperation;
import mcsm.Model.Goods;
import mcsm.Model.SaleUnit;

public class EditSaleUnitConnect implements EditOperation {

	private ArrayList<SaleUnit> saleUnits;
	private SaleUnit saleUnit;
	
	public EditSaleUnitConnect(SaleUnit saleUnit) {
		this.saleUnit = saleUnit;
		this.saleUnits = new ArrayList<SaleUnit>();
		this.saleUnits.add(saleUnit);
	}
	
	public EditSaleUnitConnect(ArrayList<SaleUnit> saleUnits) {
		this.saleUnits = saleUnits;
	}

	@Override
	public Boolean connect(Statement statement) {
		boolean isOK = true;
		String[] edit = edit();
		for(int i = 0; i < edit.length; i++) {
			try {
				statement.executeUpdate(edit[i]);
			} catch (SQLException e) {
				isOK = false;
				e.printStackTrace();
			}
		}
		return isOK;
	}

	@Override
	public String[] edit() {
		String saleUnitID = saleUnit.getSaleUnitID();
		String cashierID = saleUnit.getcashierID();
		ArrayList<Goods> goods = saleUnit.getGoodsList();
		String goodsIDs = "";
		String goodsNum = "";
		for(int j = 0; j < goods.size(); j++) {
			if(goodsIDs.equals("")) {
				goodsIDs = goodsIDs + goods.get(j).getGoodsID();
				goodsNum = goodsNum + goods.get(j).getQuantity();
			}else {
				goodsIDs = goodsIDs + ", " + goods.get(j).getGoodsID(); 
				goodsNum = goodsNum + ", " + goods.get(j).getQuantity();
			}
		}
		
		ArrayList<Goods> gifts = saleUnit.getGiftList();
		String giftsIDs = "";
		String giftsNum = "";
		if(gifts.size() != 0) {
			for(int j = 0; j < gifts.size(); j++) {
				if(giftsIDs.equals("")) {
					giftsIDs = giftsIDs + gifts.get(j).getGoodsID();
					giftsNum = giftsNum + gifts.get(j).getQuantity();
				}else {
					giftsIDs = giftsIDs + ", " + gifts.get(j).getGoodsID();
					giftsNum = giftsNum + ", " + gifts.get(j).getQuantity();
				}
			}
		}
		
		double totalPrice = saleUnit.getTotalPrice();
		double ticketGot = saleUnit.getTotalTicketGot();
		double ticketUsed = saleUnit.getTicketUsed();
		boolean hasSended = saleUnit.hasSendedGift();
		String date = saleUnit.getSaleDate();
		String vipID = saleUnit.getVIPID();
		
		String[] sql = new String[11];
		
		sql[0] = "update saleunit set cashierID = '" + cashierID + "' where id = " + saleUnitID;
		sql[1] = "update saleunit set goodsList = '" + goodsIDs + "' where id = " + saleUnitID;
		sql[2] = "update saleunit set giftList = '" + giftsIDs + "' where id = " + saleUnitID;
		sql[3] = "update saleunit set totalPrice = " + totalPrice + " where id = " + saleUnitID;
		sql[4] = "update saleunit set ticketGot = " + ticketGot + " where id = " + saleUnitID;
		sql[5] = "update saleunit set ticketUsed = " + ticketUsed + " where id = " + saleUnitID;
		sql[6] = "update saleunit set hasSendedGift = " + hasSended + " where id = " + saleUnitID;
		sql[7] = "update saleunit set date = '" + date + "' where id = " + saleUnitID;
		sql[8] = "update saleunit set goodsNum = '" + goodsNum + "' where id = " + saleUnitID;
		sql[9] = "update saleunit set giftNum = '" + giftsNum + "' where id = " + saleUnitID;
		sql[10] = "update saleunit set vipid = '" + vipID + "' where id = " + saleUnitID;
		
		return sql;
	}

}
